package net.telesing.tcmc.back.action;

import net.telesing.tcmc.back.logic.OrganizationLogic;
import net.telesing.tcmc.back.model.OrganizationModel;
import net.telesing.tcmc.back.model.UserModel;
import net.telesing.tcmc.back.modelqp.OrganizationQp;
import net.telesing.tcmc.common.Constants;
import net.telesing.tcmc.common.LogicException;
import net.telesing.tcmc.common.QueryReturn;
import net.telesing.tcmc.core.web.Ajax;
import net.telesing.tcmc.core.web.AjaxBean;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;

import java.util.HashMap;
import java.util.Map;


/**
 * 机构信息管理api
 * Created by bys on 2015/11/3.
 */
@At("/org")
@IocBean
public class OrganizationAction
{
    private static final Logger log = LogManager.getLogger(OrganizationAction.class);
    @Inject
    private OrganizationLogic organizationLogic;

    /**
     * 跳转到机构信息管理页面
     * 根据条件查询
     *
     * @param qp
     * @return
     */

    @At
    //    @POST
    @Ok("jsp:${obj.jsp}")
    public Map<String, Object> doList(@Param("flg") int flg,@Param("..") OrganizationQp qp, @Attr(Constants.SESSION_KEY) UserModel op)
    {
        log.info("org>>>>>>doList");
        Map<String, Object> map = new HashMap<String, Object>();
        if (qp.getPage() == 0)
        {
            qp.setPage(1);
        }
        if(flg == 2){
            qp.setRows(10);
        }else {
            qp.setRows(20);
        }

        map.put("queryReturn", organizationLogic.doList(qp, op));
        map.put("page", qp.getPage());
        map.put("jsp", organizationLogic.getJsp(flg));
        return map;
    }

    @At
    //    @POST
    @Ok("jsp:pages.back.organization.new_modify_org")
    public Map<String, Object> findId(@Param("id") long id, @Attr(Constants.SESSION_KEY) UserModel op)
    {
        log.info("org>>>>>>findId");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("organizationModel", organizationLogic.find(id));
        return map;
    }

    /**
     * 机构信息新增
     *
     * @param organizationModel
     * @param op
     * @return
     */
    @At
    @POST
    @Ok("json")
    public AjaxBean create(@Param("..") OrganizationModel organizationModel, @Attr(Constants.SESSION_KEY) UserModel op)
    {
        log.info("org>>>>>>create");
        try
        {
            return Ajax.ok().setData(organizationLogic.create(organizationModel, op.getId()));
        }
        catch (LogicException e)
        {
            return Ajax.fail().setMsg(e.getMessage());
        }
    }

    /**
     * 机构信息修改
     *
     * @param organizationModel
     * @param op
     * @return
     */
    @At
    @POST
    @Ok("json")
    public AjaxBean modify(@Param("..") OrganizationModel organizationModel, @Attr(Constants.SESSION_KEY) UserModel op)
    {
        log.info("org>>>>>>modify");
        try
        {
            return Ajax.ok().setData(organizationLogic.modify(organizationModel, op.getId()));
        }
        catch (LogicException e)
        {
            return Ajax.fail().setMsg(e.getMessage());
        }
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     * @throws LogicException
     */
    @At
    @POST
    @Ok("json")
    public AjaxBean delete(@Param("ids") long[] ids, @Attr(Constants.SESSION_KEY) UserModel op) throws LogicException
    {
        log.info("org>>>>>>delete");
        if (ids.length != 0)
        {
            try
            {
                organizationLogic.delete(ids, op.getId());
                return Ajax.ok();
            }
            catch (LogicException e)
            {
                return Ajax.fail().setMsg(e.getMessage());
            }
        }
        else
        {
            return Ajax.fail().setMsg("请选择删除项！");
        }
    }
}
